package cc.mcggdd.leah.boot.stock.service.impl;

import cc.mcggdd.leah.boot.core.security.util.SecurityUtils;
import cc.mcggdd.leah.boot.stock.model.entity.StockBill;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.mcggdd.leah.boot.stock.mapper.StockTradingMapper;
import cc.mcggdd.leah.boot.stock.service.StockTradingService;
import cc.mcggdd.leah.boot.stock.model.entity.StockTrading;
import cc.mcggdd.leah.boot.stock.model.form.StockTradingForm;
import cc.mcggdd.leah.boot.stock.model.query.StockTradingQuery;
import cc.mcggdd.leah.boot.stock.model.vo.StockTradingVO;
import cc.mcggdd.leah.boot.stock.converter.StockTradingConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 交易明细服务实现类
 *
 * @author 
 * @since 2025-02-13 10:16
 */
@Service
@RequiredArgsConstructor
public class StockTradingServiceImpl extends ServiceImpl<StockTradingMapper, StockTrading> implements StockTradingService {

    private final StockTradingConverter stockTradingConverter;

    /**
    * 获取交易明细分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<StockTradingVO>} 交易明细分页列表
    */
    @Override
    public IPage<StockTradingVO> getStockTradingPage(StockTradingQuery queryParams) {
        Page<StockTradingVO> pageVO = this.baseMapper.getStockTradingPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取交易明细表单数据
     *
     * @param id 交易明细ID
     * @return
     */
    @Override
    public StockTradingForm getStockTradingFormData(Long id) {
        StockTrading entity = this.getById(id);
        return stockTradingConverter.toForm(entity);
    }
    
    /**
     * 新增交易明细
     *
     * @param formData 交易明细表单对象
     * @return
     */
    @Override
    public boolean saveStockTrading(StockTradingForm formData) {
        StockTrading entity = stockTradingConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserId());
        return this.save(entity);
    }
    
    /**
     * 更新交易明细
     *
     * @param id   交易明细ID
     * @param formData 交易明细表单对象
     * @return
     */
    @Override
    public boolean updateStockTrading(Long id,StockTradingForm formData) {
        StockTrading entity = stockTradingConverter.toEntity(formData);
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }
    
    /**
     * 删除交易明细
     *
     * @param ids 交易明细ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteStockTradings(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的交易明细数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public Long getCountByHoldingId(Long holdingId) {
        return this.baseMapper.getCountByHoldingId(holdingId);
    }

    @Override
    public List<StockTradingVO> selectStockTradingListByHoldingId(Long holdingId) {
        return this.baseMapper.selectStockTradingListByHoldingId(holdingId);
    }

}
